﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using Greenshot2Core.Interfaces;
using System.IO;

namespace Greenshot2.Capture {
	/// <summary>
	/// Interaktionslogik für CaptureWindow.xaml
	/// </summary>
	public partial class CaptureWindow : Window {
        double x1,y1,x2,y2;
		Point position = new Point(int.MinValue, int.MinValue);
		DispatcherTimer dispatcherTimer;

		bool isMouseDown = false;

		public Int32Rect Selection {
			get;
			set;
		}

		public CaptureWindow(CaptureContainer capture) {
            InitializeComponent();
			this.DataContext = capture;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e) {
			Top = SystemParameters.VirtualScreenTop;
			Left = SystemParameters.VirtualScreenLeft;
			Width = SystemParameters.VirtualScreenWidth;
			Height = SystemParameters.VirtualScreenHeight;
            MCrossHorizontal.X1 = 0;
            MCrossHorizontal.X2 = Magnifier.ActualWidth;
            MCrossHorizontal.Y1 = MCrossHorizontal.Y2 = Magnifier.ActualHeight / 2;
            MCrossVertical.X1 = MCrossVertical.X2 = Magnifier.ActualWidth / 2;
            MCrossVertical.Y1 = 0;
            MCrossVertical.Y2 = Magnifier.ActualHeight;

			dispatcherTimer = new DispatcherTimer();
			dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
			dispatcherTimer.Interval = TimeSpan.FromMilliseconds(20);
			dispatcherTimer.Start();
        }

        private void Canvas_MouseMove(object sender, MouseEventArgs e) {
            // Get the x and y coordinates of the mouse pointer.
            position = e.GetPosition(this);
			if (dispatcherTimer == null) {
				dispatcherTimer_Tick(null, null);
			}
		}

		private void dispatcherTimer_Tick(object sender, EventArgs e) {
			if (position.X == int.MinValue) {
				return;
			}

			double pX = position.X;
			double pY = position.Y;
			// Draw selected
            if (isMouseDown) {
                Canvas.SetLeft(SelectedArea, Math.Min(x1, pX));
                Canvas.SetTop(SelectedArea, Math.Min(y1, pY));
                SelectedArea.Width = Math.Abs(pX - x1);
                SelectedArea.Height = Math.Abs(pY - y1);
            }
            // Set the "cross-hair"
            CrossHorizontal1.X1 = 0;
            CrossHorizontal1.Y1 = pY;
            CrossHorizontal1.X2 = pX - 10;
            CrossHorizontal1.Y2 = pY;
            CrossHorizontal2.X1 = pX + 10;
            CrossHorizontal2.Y1 = pY;
            CrossHorizontal2.X2 = Capture.ActualWidth;
            CrossHorizontal2.Y2 = pY;

            CrossVertical1.X1 = pX;
            CrossVertical1.Y1 = 0;
            CrossVertical1.X2 = pX;
            CrossVertical1.Y2 = pY - 10;
            CrossVertical2.X1 = pX;
            CrossVertical2.Y1 = pY + 10;
            CrossVertical2.X2 = pX;
            CrossVertical2.Y2 = Capture.ActualHeight;

            int magnifiedAreaWidth = 30;
            int magnifiedAreaHeight = 30;
            int xOffset = 10;
            int yOffset = 10;

            // Sets the position of the image to the mouse coordinates.
            if (pX + xOffset + Magnifier.ActualWidth >= Capture.ActualWidth) {
                xOffset = (int)(-xOffset - Magnifier.ActualWidth);
            }
            if (pY + yOffset + Magnifier.ActualHeight >= Capture.ActualHeight) {
                yOffset = (int)(-yOffset - Magnifier.ActualHeight);
            }
            Magnifier.SetValue(Canvas.LeftProperty, pX + xOffset);
            Magnifier.SetValue(Canvas.TopProperty, pY + yOffset);
			MagnifiedContent.Viewbox = new Rect(new Point(pX - (magnifiedAreaWidth / 2), pY - (magnifiedAreaHeight / 2)), new Size(magnifiedAreaWidth, magnifiedAreaHeight));
        }

        private void Canvas_MouseDown(object sender, MouseButtonEventArgs e) {
            isMouseDown = true;
            System.Windows.Point position = e.GetPosition(this);
            x1 = position.X;
            y1 = position.Y;
            SelectedArea.Visibility = Visibility.Visible;
        }

        private void Canvas_MouseUp(object sender, MouseButtonEventArgs e) {
            isMouseDown = false;
            System.Windows.Point position = e.GetPosition(this);
            x2 = position.X;
            y2 = position.Y;

            Selection = new Int32Rect((int)Math.Min(x1, x2), (int)Math.Min(y1, y2), (int)Math.Abs(x1 - x2), (int)Math.Abs(y1 - y2));
			if (Selection.Width * Selection.Height > 0) {
                DialogResult = true;
            }
        }

        private void Window_KeyDown(object sender, KeyEventArgs e) {
            if (e.Key == Key.Escape) {
                Close();
            }
        }
    }
}